{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tkinter\n",
    "import tkinter.messagebox\n",
    "import tkinter.ttk\n",
    "root=tkinter.Tk()\n",
    "root.title('Selection widgets')\n",
    "root['height']=400\n",
    "root['width']=320\n",
    "varName=tkinter.StringVar()\n",
    "varName.set('')\n",
    "labelName=tkinter.Label(root,text='Name:',justify=tkinter.RIGHT,width=50)\n",
    "labelName.place(x=10,y=10,width=50,height=20)\n",
    "entryName=tkinter.Entry(root,width=120,textvariable=varName)\n",
    "entryName.place(x=70,y=5,width=120,height=20)\n",
    "labelGrade=tkinter.Label(root,text='Grade:',justify=tkinter.RIGHT,width=50)\n",
    "labelGrade.place(x=10,y=40,width=50,height=20)\n",
    "studentClasses={'1':['1','2','3','4'],'2':['1','2'],'3':['1','2','3']}\n",
    "comboxGrade=tkinter.ttk.Combobox(root,width=50,value=list(studentClasses.keys()))\n",
    "comboxGrade.place(x=70,y=40,width=50,height=20)\n",
    "def comboClassChange(event):\n",
    "    grade=comboxGrade.get()\n",
    "    if grade:\n",
    "        comboClass['value']=studentClasses(grade)\n",
    "    else:\n",
    "        comboClass.set([])\n",
    "comboxGrade.bind('<<ComboboxSelected>>',comboClassChange)\n",
    "labelClass=tkinter.Label(root,text='Class:',justify=tkinter.RIGHT,width=50)\n",
    "labelClass.place(x=130,y=40,width=50,height=20)\n",
    "comboClass=tkinter.ttk.Combobox(root,width=50)\n",
    "comboClass.place(x=190,y=40,width=50,height=20)\n",
    "labelSex=tkinter.Label(root,text='Sex:',justify=tkinter.RIGHT,width=50)\n",
    "labelSex.place(x=10,y=70,width=50,height=20)\n",
    "sex=tkinter.IntVar()\n",
    "sex.set(1)\n",
    "radioMale=tkinter.Radiobutton(root,variable=sex,value=1,text='Man')\n",
    "radioMale.place(x=70,y=70,width=50,height=20)\n",
    "radioFemale=tkinter.Radiobutton(root,variable=sex,value=0,text='Woman')\n",
    "radioFemale.place(x=130,y=70,width=70,height=20)\n",
    "#与是否班长相关的变量，默认当前学生不是班长\n",
    "monitor=tkinter.IntVar()\n",
    "monitor.set(0)\n",
    "#复选框，选中为1，未选中为0\n",
    "checkMonitor=tkinter.Checkbutton(root,text='Is Monitor?',variable=monitor,onvalue=1,offvalue=0)\n",
    "checkMonitor.place(x=20,y=100,width=100,height=20)\n",
    "#添加按钮单击事件处理函数\n",
    "def addInformation():\n",
    "    result='Name:'+entryName.get()\n",
    "    result=result+';Grade:'+comboGrade.get()\n",
    "    result=result+';Class:'+comboClass.get()\n",
    "    result=result+';Sex:'+('Man' if sex.get() else 'Woman')\n",
    "    result=result+';Monitor:'+('Yes' if monitor.get() else 'No')\n",
    "    #把信息插入到列表框中组件中\n",
    "    listboxStudents.insert(0,result)\n",
    "buttonAdd=tkinter.Button(root,text='Add',width=40,command=addInformation)\n",
    "#删除按钮的事件处理函数\n",
    "def deleteSelection():\n",
    "    selection=listboxStudents.curselection()\n",
    "    if not selection:\n",
    "        #删除按钮的事件处理函数\n",
    "        def deleteSelection():\n",
    "            selection=listboxStudents.curselection()\n",
    "            if not selection:\n",
    "                tkinter.messagebox.showinfo(title='Information',message='No Selection')\n",
    "            else:\n",
    "                listboxStudents.delete(selection)\n",
    "        buttonDelete=tkinter.Button(root,text='DeleteSelection',width=100,command=deleteSelection)\n",
    "        buttonDelete.place(x=180,y=100,width=100,height=20)\n",
    "listboxStudents=tkinter.Listbox(root,width=300)\n",
    "listboxStudents.place(x=10,y=130,width=300,height=200)\n",
    "root.mainloop()\n",
    "#                                                                                                     马东升\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
